<?php
namespace app\api\controller\food\market;

use app\api\controller\food\Controller;
use app\api\model\Setting as SettingModel;
use app\api\model\food\Record as RecordModel;
use hema\wechat\Pay as WxPay;
use think\facade\Cache;

/**
 * 用户充值管理
 */
class Recharge extends Controller
{
	private $user;

    /**
     * 构造方法
     */
    public function initialize()
    {
        parent::initialize();
        $this->user = $this->getUserDetail();   // 用户信息
    } 
	
    /**
     * 充值支付
     */
    public function pay($money,$recharge_plan_id = 0)
    {
        $order_no = order_no();
        $order = [
            'order_no' => $order_no,
            'money' => $money,
            'recharge_plan_id' => $recharge_plan_id,
            'remark' => '用户充值',
            'user_id' => $this->user['user_id'],
            'shop_id' => $this->shop_id,
            'applet_id' => $this->applet_id
        ];
        Cache::set($order_no, $order,7200);
        $wx = new WxPay(SettingModel::getItem('wxpay'));
        $wxParams = $wx->jsapi($order_no,$money,$this->user['open_id'],'api/food.notify/recharge/appletid/'.$this->applet_id,'用户充值');
        return $this->renderSuccess($wxParams);
    }
    
    /**
     * 充值记录
     */
    public function logs()
    {
        $model = new RecordModel;
        $list = $model->getList([
            'user_type' => 10,
            'action' => 10,
            'type' => 10,
            'user_id' => $this->user['user_id'],
        ]);
        return $this->renderSuccess(compact('list'));
    }
}
